package com.neusoft.clouduserfeign.controller;

import com.neusoft.clouduserfeign.dto.LoginRequest;
import com.neusoft.clouduserfeign.dto.Result;
import com.neusoft.clouduserfeign.dto.User;
import com.neusoft.clouduserfeign.feign.UserServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户服务Feign控制器
 * 通过Feign客户端调用用户服务，实现负载均衡
 */
@RestController
@RequestMapping("/feign/users")
public class UserFeignController {
    
    @Autowired
    @Qualifier("com.neusoft.clouduserfeign.feign.UserServiceClient")
    private UserServiceClient userServiceClient;
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<User> login(@RequestBody LoginRequest request) {
        try {
            System.out.println("Feign Controller - 接收到登录请求: " + request);
            Result<User> result = userServiceClient.login(request);
            System.out.println("Feign Controller - 用户服务响应: " + result);
            return result;
        } catch (Exception e) {
            System.err.println("Feign Controller - 登录异常: " + e.getMessage());
            e.printStackTrace();
            return Result.error("登录服务异常: " + e.getMessage());
        }
    }
    
    /**
     * 创建用户
     */
    @PostMapping
    public Result<Void> createUser(@RequestBody User user, @RequestParam("createBy") String createBy) {
        return userServiceClient.createUser(user, createBy);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{userId}")
    public Result<Void> updateUser(@PathVariable("userId") String userId,
                                  @RequestBody User user,
                                  @RequestParam("updateBy") String updateBy) {
        return userServiceClient.updateUser(userId, user, updateBy);
    }

    /**
     * 根据用户ID查询用户
     */
    @GetMapping("/{userId}")
    public Result<User> getUserByUserId(@PathVariable("userId") String userId) {
        return userServiceClient.getUserByUserId(userId);
    }
    
    /**
     * 删除用户（逻辑删除）
     */
    @DeleteMapping("/{userId}")
    public Result<Void> deleteUser(@PathVariable("userId") String userId, @RequestParam("updateBy") String updateBy) {
        return userServiceClient.deleteUser(userId, updateBy);
    }

    /**
     * 查询用户列表
     */
    @GetMapping
    public Result<List<User>> getUserList(@RequestParam(value = "userType", required = false) String userType,
                                         @RequestParam(value = "status", required = false) String status,
                                         @RequestParam(value = "keyword", required = false) String keyword) {
        return userServiceClient.getUserList(userType, status, keyword);
    }

    /**
     * 启用/禁用用户
     */
    @PutMapping("/{userId}/status")
    public Result<Void> updateUserStatus(@PathVariable("userId") String userId,
                                        @RequestParam("status") String status,
                                        @RequestParam("updateBy") String updateBy) {
        return userServiceClient.updateUserStatus(userId, status, updateBy);
    }

    /**
     * 修改密码
     */
    @PutMapping("/{userId}/password")
    public Result<Void> changePassword(@PathVariable("userId") String userId,
                                      @RequestBody Object request,
                                      @RequestParam("updateBy") String updateBy) {
        return userServiceClient.changePassword(userId, request, updateBy);
    }

    /**
     * 重置密码（管理员操作）
     */
    @PutMapping("/{userId}/reset-password")
    public Result<Void> resetPassword(@PathVariable("userId") String userId,
                                     @RequestBody Object request,
                                     @RequestParam("updateBy") String updateBy) {
        return userServiceClient.resetPassword(userId, request, updateBy);
    }

    /**
     * 检查权限
     */
    @GetMapping("/{userId}/permission")
    public Result<Boolean> checkPermission(@PathVariable("userId") String userId,
                                          @RequestParam(value = "operation", required = false) String operation) {
        return userServiceClient.checkPermission(userId, operation);
    }
    

}
